/*
 * Copyright 2007 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.jtell.internal.config.xml;

import org.jtell.internal.Guard;

/**
 * <p>
 * <code>OrderElement</code> accumulates and holds the information in an <code>&lt;after ...&gt;</code> or
 * <code>&lt;before ...&gt;</code> configuration element.
 * </p>
 * <p>
 * <strong>Thread Safety</strong><br/>
 * Instances of this class are safe for multithreaded access.
 * </p>
 */
public class OrderElement
{
    /**
     * <p>
     * Precalculated instance hash code. 
     * </p>
     */
    private final int m_hashCode;

    /**
     * <p>
     * The token string. 
     * </p>
     */
    private final String m_token;

    /**
     * <p>
     * The required flag. 
     * </p>
     */
    private final boolean m_required;

    /**
     * <p>
     * Construct a {@link OrderElement} instance.
     * </p>
     *
     * @param token the token string.
     * @param required the required flag.
     */
    public OrderElement(final String token, final boolean required)
    {
        super();
        Guard.notNull("token", token);

        m_required = required;
        m_token = token;
        m_hashCode = generateHashCode();
    }

    /* inherit javadoc */
    public int hashCode()
    {
        return m_hashCode;
    }

    /* inherit javadoc */
    public boolean equals(final Object o)
    {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        OrderElement that = (OrderElement) o;

        if (m_required != that.m_required) return false;
        if (!m_token.equals(that.m_token)) return false;

        return true;
    }

    public String getToken()
    {
        return m_token;
    }

    public boolean isRequired()
    {
        return m_required;
    }

    /**
     * <p>
     * Generate the instance hash code.
     * </p>
     *
     * @return <code>int</code> hash code value.
     */
    private int generateHashCode()
    {
        int result;
        result = m_token.hashCode();
        result = 31 * result + (m_required ? 1 : 0);
        return result;
    }
}
